﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using System.Web.Routing;

namespace Sub.TaskPlanningDesk.WebSite.Infrastructure
{
	public class UnityControllerFactory : IControllerFactory
	{
		private IUnityContainer _container;
		private IControllerFactory _innerFactory;

		public UnityControllerFactory(IUnityContainer container):this(container, new DefaultControllerFactory()){}
		protected UnityControllerFactory(IUnityContainer container, IControllerFactory innerFactory)
		{
			_container = container;
			_innerFactory = innerFactory;
		}

		public IController CreateController(RequestContext requestContext, string controllerName)
		{
			try
			{
				return _container.Resolve<IController>(controllerName.ToLowerInvariant());
			}
			catch (Exception)
			{
				return _innerFactory.CreateController(requestContext, controllerName);
			}
		}

		public void ReleaseController(IController controller)
		{
			_container.Teardown(controller);
		}

		public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
		{
			return System.Web.SessionState.SessionStateBehavior.Default;
		}
	}
}